// 全局响应拦截器
import {
  Injectable,
  NestInterceptor,
  CallHandler,
  ExecutionContext,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import type { Observable } from 'rxjs';

interface data<T> {
  data: T;
}

@Injectable()
export class ResponseIntercept<T = any> implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<data<T>> {
    return next.handle().pipe(
      map((data) => {
        console.log('触发全局响应拦截器', data);

        return {
          // data: data.data, // 这里的data 就是控制器，return的东西
          data,
          // state: data.code === '1' ? '1' : '0',
          state: '1',
          errmsg: null,
          errcode: null,
          errstack: null,
        };
      }),
    );
  }
}
